layui.define('my_send', function(exports){
    var showIndex1,showIndex2,showIndexParent=0,fMcIdLast,fMcRemarkLast;
    var $ = layui.$,
        upload = layui.upload,
        http_proxy = layui.http_proxy,
        step = layui.step,
        form = layui.form,
        setter = layui.setter,
        laytpl = layui.laytpl;
    var apendTr = '';

    //按钮权限控制
    var getTpl = buttonTpl.innerHTML;
    var view = document.getElementById("buttonArea");
    var data = {
        import_button: $.inArray('equipment_import_page:import_excel',layui.data(setter.tableName).user_info.menuAndPermissionList),
        download_button: $.inArray('equipment_import_page:download_template',layui.data(setter.tableName).user_info.menuAndPermissionList),
        input_button: $.inArray('equipment_input_page:input',layui.data(setter.tableName).user_info.menuAndPermissionList)
    }
    laytpl(getTpl).render(data,function(html){
        view.innerHTML = html;
    })

    $.ajax({
        url: http_proxy('/devStatus/getDomain'),
        type:'GET',
        datatype:'json',
        async:false,
        success:function(data){
            $.each( data.result, function (i, item){
                // var indexParames = item.fMcRemark+'!#!%!'+item.fMcId+'!#!%!'+item.fParent;
                var indexParames = item.label+'!#!%!'+item.value;
                apendTr += '<li onclick = demoTest("'+indexParames+'",this);>' +item.label + '<i class="layui-icon layui-icon-next"></i></li>'
            })
            $('.importItem1 ul').html(apendTr);
        }
    });
    form.on('submit(formStep)', function (data) {
        step.next('.layui-step');
        return false;
    });
    form.on('submit(formStep2)', function (data) {
        var field = data.field; //获取提交的字段
        console.log(field);
        field.fMcId = fMcIdLast;
        var domainArray = field.domain
            ,systemNameArray = field.system_name
            ,etNameArray = field.et_name
            ,chekDomain = domainArray.indexOf("#@***#!-")
            ,cheksystemName = systemNameArray.indexOf("#@***#!-")
            ,cheketName = etNameArray.indexOf("#@***#!-")
            ,domainStr = field.domain
            ,systemNameStr = field.system_name
            ,etNameStr = field.et_name;
        if(chekDomain!=-1||chekDomain!='-1'){
            domainStr = domainArray.split("#@***#!-")[0];
        }
        if(cheksystemName!=-1||cheksystemName!='-1'){
            systemNameStr = systemNameArray.split("#@***#!-")[0];
        } 
        if(cheketName!=-1||cheketName!='-1'){
            etNameStr = etNameArray.split("#@***#!-")[0];
        }
        field.domain = domainStr;
        field.system_name = systemNameStr;
        field.et_name = etNameStr;
        $.ajax({
            url:http_proxy('/et/saveAttrValue'),
            type:'post',
            data:field,
            success:function(data){
                if(data.code=='success' ){
                    layer.msg(data.message,{icon:6});

                }else{
                    layer.msg(data.message, {icon: 5});
                }
            }
        });
        $("#domain").html(domainStr);
        $("#system_name").html(systemNameStr);
        $("#et_name").html(etNameStr);
        // step.next('.layui-step');
        return false;
    });
    // form.on('submit(layuiadmin-app-input-submit)', function(data){
    //     console.log("submit...........>>>>进行中......"+fMcIdLast);
    //     var field = data.field; //获取提交的字段
    //     field.fMcId = fMcIdLast;
    //     $.ajax({
    //         url:http_proxy('/et/saveAttrValue'),
    //         type:'post',
    //         data:field,
    //         success:function(data){
    //             if(data.code=='success' ){
    //                 layer.msg(data.message,{icon:6});
    //             }else{
    //                 layer.msg(data.message, {icon: 5});
    //             }
    //         }
    //     });
    // });
    upload.render({
        elem: '#import-excel'
        , url: http_proxy('/et/importProduct')
        , accept: 'file' //普通文件
        ,exts:'xls'
        ,size:1024
        , type: "json"
        , before: function (obj) {
            layer.msg("请求中。。。请稍后.",{icon: 6, time: 4000})
        }
        , done: function (data) {
            layer.closeAll("loading");
            console.log(data);
            if (data.code == "success") {
                var msg = "状态："+data.code+","+data.message;
                layer.msg(msg, {icon: 6, time: 4000});
                // layer.msg();
                table.reload('pallet-table', {page: {curr: 1}});
            } else {
                layer.msg("状态：" + data.code+","+data.message, {icon: 5,time: 4000});
            }
        }
    });
    exports('equipmentInput', {});
})
function demoTest(id,obj){
    var $ = layui.$, http_proxy = layui.http_proxy;
    var paramesArrIndex = id.split('!#!%!');
    showIndexParent=paramesArrIndex[1];
    $('.importItem3 ul').empty();
    $(obj).addClass("active");
    $(obj).siblings().removeClass('active');
    $.ajax({
        // url: http_proxy('/et/getFMcByParams?fMcRemark=&fParent=') + paramesArrIndex[1],
        url:http_proxy('/devStatus/getSystem?parentId=')+ showIndexParent,
        type:'GET',
        datatype:'json',
        async:false,
        success:function(data){
            var apendTr = '';
            fMcRemarkLast=paramesArrIndex[0];
            fMcIdLast=paramesArrIndex[1];
            $("#showNameSpan").html('当前选中为：'+paramesArrIndex[0]);
            $.each( data.result, function (i, item){
                var parames = item.label+"-***-"+item.value;
                apendTr += '<li onclick = demoTestt("'+ parames +'",this)>' +item.label + '<i class="layui-icon layui-icon-next"></i></li>'
            })
            $('.importItem2 ul').html(apendTr);
            showIndex1=paramesArrIndex[0];
        }
    })
}
function demoTestt(idd,obj){
    var $ = layui.$, http_proxy = layui.http_proxy;
    $(obj).addClass("active");
    $(obj).siblings().removeClass('active');
    var paramesArr = idd.split('-***-');
    showIndexParent = paramesArr[1];
    $.ajax({
        url:http_proxy('/devStatus/getDevice?parentId=')+showIndexParent,
        type:'GET',
        datatype:'json',
        async:false,
        success:function(data){
            fMcRemarkLast=paramesArr[0];
            fMcIdLast=paramesArr[1];
            $("#showNameSpan").html('当前选中为：'+paramesArr[0]);
            var apendTr = '';
            $.each( data.result, function (i, item){
                var parames1 = item.label+"-***-"+item.value;
                apendTr += '<li onclick = demoTesttt("'+ parames1 +'",this)>' +item.label + '<i class="layui-icon layui-icon-next"></i></li>'
            })
            $('.importItem3 ul').html(apendTr);
            showIndex2=paramesArr[0];
        }
    })
}
function downloadCateTemplate(){
    var $ = layui.$, http_proxy = layui.http_proxy;
    // let params = "?fMcId=" + fMcIdLast + "&fMcRemark=" + fMcRemarkLast;
    window.location.href = http_proxy("/ExcelModel.xls");
}
 function searchFMc(){
    var $ = layui.$, http_proxy = layui.http_proxy;
    var searchStr = $("#searchText").val();
    $('.importItem1 ul').empty();
    $('.importItem2 ul').empty();
    $('.importItem3 ul').empty();
    $.ajax({
        url: http_proxy('/et/getFMcByParams'),
        type:'GET',
        datatype:'json',
        async:false,
        data:{"fMcRemark":searchStr},
        success:function(data){
            if(data.grid.rows==null||data.grid.rows==""){
              $('.importItem1 ul').children().filter('li').remove();
            }
             var str = '';
            $.each( data.grid.rows, function (i, item){
                if(item.fLevel == 0){
                      var htmlStr = $('.importItem1 ul').html();
                      str = htmlStr+'<li class = "item'+item.fMcId+'">' +item.fMcRemark + '<i class="layui-icon layui-icon-next"></i></li>';
                      $('.importItem1 ul').html(str);
                      var indexParamesSearch1 = item.fMcRemark+'!#!%!'+item.fMcId+'!#!%!'+item.fParent;
                      console.log(indexParamesSearch1);
                      $(".item"+item.fMcId).attr("onclick","demoTest("+"'"+indexParamesSearch1+"'"+")");
                   }else if(item.fLevel == 1){
                    var htmlStr = $('.importItem2 ul ').html();
                    str = htmlStr+'<li class = "item'+item.fMcId+'">' +item.fMcRemark + '<i class="layui-icon layui-icon-next"></i></li>';
                    $('.importItem2 ul').html(str);
                     var indexParamesSearch2 = item.fMcRemark+'-***-'+item.fMcId+'-***-'+item.fParent;
                    $(".item"+item.fMcId).attr("onclick","demoTestt("+"'"+indexParamesSearch2+"'"+")");
                  }else{
                    var htmlStr = $('.importItem3 ul').html();
                    str = htmlStr+'<li class = "item'+item.fMcId+'">' +item.fMcRemark + '<i class="layui-icon layui-icon-next"></i></li>';
                    $('.importItem3 ul').html(str);
                     var indexParamesSearch3 = item.fMcRemark+'-***-'+item.fMcId+'-***-'+item.fParent;
                    $(".item"+item.fMcId).attr("onclick","demoTesttt("+"'"+indexParamesSearch3+"'"+")");
                  }
              })
          }
     })
}
function demoTesttt(parames1,obj){
    var $ = layui.$, http_proxy = layui.http_proxy;
    $(obj).addClass("active");
    $(obj).siblings().removeClass('active');
    var paramesArry1 = parames1.split('-***-');
    showIndexParent = paramesArry1[3];
    fMcRemarkLast=paramesArry1[0];
    fMcIdLast=paramesArry1[1];
    $("#showNameSpan").html('当前选择为：'+showIndex1+'/'+showIndex2+'/'+paramesArry1[0]);
    console.log("submit...........>>>>进行中......"+fMcIdLast);
}


